//
//  KLSGCollectionViewController.m
//  KaoLaShiGuangFM
//
//  Created by lanou3g on 15/11/2.
//  Copyright © 2015年 com.lanou3g. All rights reserved.
//

#import "KLSGCollectionViewController.h"
#import "KLSGCollectionCell.h"
#import "KLSGDB.h"
#import "KLSGBrifAndVoiceViewController.h"
#import "AppDelegate.h"

@interface KLSGCollectionViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic,strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) KLSGDB *dbManager;

@end

@implementation KLSGCollectionViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    if (self.dataArray.count == 0) {
        UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, 50)];
        lable.text = @"收藏空空如也,赶紧去收藏吧 ╮(╯_╰)╭";
        lable.textAlignment = NSTextAlignmentCenter;
        lable.font = [UIFont systemFontOfSize:14];
        //        lable.backgroundColor = [UIColor redColor];
        lable.alpha = kAlpha;
        [self.view addSubview:lable];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
   self.title = @"我的收藏";
    self.navigationController.navigationBar.hidden = NO;
    [self addSubViews];
    [self setupData];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - 加载数据
- (void)setupData
{
    self.dbManager = [[KLSGDB alloc] init];
    self.dataArray = [NSMutableArray arrayWithArray:[_dbManager queryAllKindModel]];
    [self.tableView reloadData];
}

#pragma mark - 创建布局
- (void)addSubViews
{
    
    UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemReply) target:self action:@selector(leftReturn)];
    left.tintColor = [UIColor colorWithRed:234 / 256.0 green:79 / 256.0 blue:56 / 256.0 alpha:1];
    self.navigationItem.leftBarButtonItem = left;
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:(UITableViewStylePlain)];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    // 注册cell
    [self.tableView registerClass:[KLSGCollectionCell class] forCellReuseIdentifier:@"collectionCell"];
  
}

// 返回到上一个页面
- (void)leftReturn
{
    [self.navigationController popViewControllerAnimated:YES];
    AppDelegate *app = [UIApplication sharedApplication].delegate;
    [app.leftSliderVC setPanEnabled:YES];
}

#pragma mark - tableView代理方法
// 返回分区行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

// 返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    KLSGCollectionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"collectionCell" forIndexPath:indexPath];
    KLSGKindModel *model = self.dataArray[indexPath.row];
    // 显示数据
    cell.picture.image = [UIImage imageNamed:@"zhanweitu"];
    [cell.picture sd_setImageWithURL:[NSURL URLWithString:model.albumCoverUrl290]];
    if (model.title == nil || model.title.length == 0 || [model.title isEqualToString:@"(null)"]) {
        model.title = @"";
    }
    if (model.intro == nil || model.intro.length == 0 || [model.intro isEqualToString:@"(null)"]) {
        model.intro = @"";
    }
    cell.title.text = model.title;
    cell.detail.text = model.intro;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 81;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    KLSGBrifAndVoiceViewController *brigAndVoiceVC = [[KLSGBrifAndVoiceViewController alloc] init];
    KLSGKindModel *model = self.dataArray[indexPath.row];
    brigAndVoiceVC.albumIdString = [NSString stringWithFormat:@"%@", model.albumId];
    brigAndVoiceVC.model = model;
    [self.navigationController pushViewController:brigAndVoiceVC animated:YES];
}

#pragma mark -删除数据
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    KLSGKindModel *model = self.dataArray[indexPath.row];
    [self.dataArray removeObjectAtIndex:indexPath.row];
    NSLog(@"%@", [NSString stringWithFormat:@"%@", model.albumId]);
    [_dbManager deleteKindWithAlbumId:model.albumId];
    [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationLeft)];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
